package com.party.admin.web.controller.crowdfund;

import com.google.common.collect.Maps;
import com.party.admin.biz.crowdfund.CrowdfundDetailCountBizService;
import com.party.admin.web.dto.AjaxResult;
import com.party.common.utils.DateUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.YesNoStatus;
import com.party.core.model.crowdfund.CrowdfundEvent;
import com.party.core.model.crowdfund.ProjectDetailCountInput;
import com.party.core.model.crowdfund.ProjectDetailCountOutput;
import com.party.core.service.crowdfund.ICrowdfundEventService;
import com.party.core.service.crowdfund.biz.ProjectDetailCountBizService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 数据分析
 * @Author: yangshoukun
 * @Date: 2018/6/12 19:23
 */
@Controller
@RequestMapping("/crowdfund/detailCount")
public class CrowdfundDetailCountController {

    private static Logger logger = LoggerFactory.getLogger(CrowdfundDetailCountController.class);

    @Autowired
    private ICrowdfundEventService crowdfundEventService;

    @Autowired
    private CrowdfundDetailCountBizService crowdfundDetailCountBizService;

    @Autowired
    private ProjectDetailCountBizService projectDetailCountBizService;

    /**
     *
     * @return
     */
    @RequestMapping("/list")
    public ModelAndView list(ProjectDetailCountInput input) throws ParseException {
        ModelAndView mv = new ModelAndView("crowdfund/crowdfundDetailCount");

        CrowdfundEvent crowdfundEvent = new CrowdfundEvent();
        crowdfundEvent.setProgress(YesNoStatus.YES.getCode()); // 进行中
        Map<String, Object> params = Maps.newHashMap();
        params.put("openStatus", 1);
        List<CrowdfundEvent> crowdfundEventList = crowdfundEventService.listWithParams(crowdfundEvent, params);

        boolean chooseAll = false;
        if ("0".equals(input.getEventId())) {
            String eventIds = crowdfundEventList.stream().map(CrowdfundEvent::getId).collect(Collectors.joining(","));
            input.setEventId(eventIds);
            chooseAll = true;
        }

        if (CollectionUtils.isNotEmpty(crowdfundEventList)) {
            CrowdfundEvent all = new CrowdfundEvent();
            all.setId("0");
            all.setName("全部");
            crowdfundEventList.add(0, all);
        }
        mv.addObject("crowdfundEventList", crowdfundEventList);
        mv.addObject("input", input);

        final String timeStart = " 00:00:00";
        final String timeEnd = " 23:59:59";
        final String pattern = "yyyy-MM-dd HH:mm:ss";
        final String datePattern = "yyyy-MM-dd";

        if (StringUtils.isEmpty(input.getApplyDateStart()) || StringUtils.isEmpty(input.getApplyDateEnd())
                || StringUtils.isEmpty(input.getEventId()) || null == input.getPeriod() || null == input.getQureyDayStart()
                || null == input.getQureyDayEnd()) {
            return mv;
        }

        String orgStart = input.getApplyDateStart();
        String orgEnd = input.getApplyDateEnd();

        if (StringUtils.isNotEmpty(input.getApplyDateStart())) {
            input.setApplyDateStart(input.getApplyDateStart().trim().concat(timeStart));
        }
        if (StringUtils.isNotEmpty(input.getApplyDateEnd())) {
            input.setApplyDateEnd(input.getApplyDateEnd().trim().concat(timeEnd));
        }

        // 处理时间段
        if (null == input.getPeriod() || input.getPeriod() < 1) {
            input.setPeriod(1);
        }

        List<ProjectDetailCountOutput> list = crowdfundDetailCountBizService.getCountByEvent(input, pattern, datePattern);

        input.setApplyDateStart(orgStart);
        input.setApplyDateEnd(orgEnd);
        if (chooseAll) {
            input.setEventId("0");
        }

        mv.addObject("list", list);

        return mv;
    }

    /**
     * 众筹事件详情
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/eventDetail")
    public AjaxResult getEventDetail(String id) {
        CrowdfundEvent crowdfundEvent = crowdfundEventService.get(id);
        Map<String, String> result = Maps.newHashMap();
        final String pattern = "yyyy-MM-dd";
        result.put("start", DateUtils.formatDate(crowdfundEvent.getCreateDate(), pattern));
        result.put("end", DateUtils.formatDate(crowdfundEvent.getEndTime(), pattern));

        return AjaxResult.success(result);
    }

    /**
     * 统计报表
     * @param eventId
     * @return
     */
    @ResponseBody
    @RequestMapping("/eventDataTable")
    public ModelAndView eventDataTable(String eventId) {
        ModelAndView mv = new ModelAndView("crowdfund/crowdfundDataTable");
        try {
            crowdfundDetailCountBizService.eventDataTable(eventId, mv);
        } catch (ParseException e) {
            logger.error("统计报表异常", e);
        }
        return mv;
    }

    @ResponseBody
    @RequestMapping("/project")
    public String project(String projectId) {
        projectDetailCountBizService.reviseProject(projectId);
        return "ok";
    }

    @ResponseBody
    @RequestMapping("/event")
    public String event(String eventId) {
        projectDetailCountBizService.reviseEvent(eventId);
        return "ok";
    }
}
